/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjtu.se1602.liyq.dao.impl;

import edu.fjtu.se1602.liyq.dao.SysUserDao;
import edu.fjtu.se1602.liyq.entity.SysUser;
import edu.fjtu.se1602.liyq.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class SysUserDaoImpl implements SysUserDao {

    @Override
    public int add(SysUser user) {
        int r = 0;
        try {
            Connection con = DBUtil.getConnection();
            PreparedStatement pst = con.prepareStatement("insert into sys_user(ACCOUNT,NAME,PASSWORD) VALUES(?,?,?)");
            pst.setString(1, user.getAccount());
            pst.setString(2, user.getName());
            pst.setString(3, user.getPassword());
            r = pst.executeUpdate();
            DBUtil.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(SysUserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public SysUser getById(int id) {
        SysUser user = null;
        try {
            Connection con = DBUtil.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from sys_user where id="+id);
            if (rs.next()) {
                user = new SysUser();
                user.setId(rs.getInt(1));
                user.setAccount(rs.getString(2));
                user.setName(rs.getString(3));
                user.setPassword(rs.getString(4));
            }
            DBUtil.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(SysUserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }

    @Override
    public List<SysUser> getList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<SysUser> getList(int page, int rowcount) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
